package com.wsz.eduservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wsz.commonutils.R;
import com.wsz.eduservice.pojo.Course;
import com.wsz.eduservice.pojo.Report;
import com.wsz.eduservice.pojo.vo.CourseQuery;
import com.wsz.eduservice.service.CourseService;
import com.wsz.eduservice.service.ReportService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Api(tags="举报管理")
@RestController
@RequestMapping("/eduservice/report")

public class EduReportContoller {

    @Autowired
    private ReportService reportService;

    @Autowired
    private CourseService courseService;

    @ApiOperation("获取举报列表")
    @PostMapping("/getReport/{current}/{limit}")
    public R getReportAll(@PathVariable long current,
                          @PathVariable long limit,
                          @RequestBody(required = false) CourseQuery courseQuery){
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("course_id");
        List<Report> list = reportService.list(null);

        List<String> list1 = new ArrayList<>();

        for (int i = 0; i < list.size(); i++) {
            Report report = list.get(i);

            list1.add(report.getCourseId());
        }
        QueryWrapper<Course> queryWrapper1 = new QueryWrapper<>();

        if(courseQuery != null) {
            String title = courseQuery.getTitle();
            Integer lessonNum = courseQuery.getLessonNum();
            BigDecimal price = courseQuery.getPrice();
            String status = courseQuery.getStatus();

            if (StringUtils.isNotBlank(title)) {
                queryWrapper1.like("title", title);
            }
            if (lessonNum != null) {
                queryWrapper1.eq("lesson_num", lessonNum);
            }
            if (price != null) {
                queryWrapper1.like("price", price);
            }
            if (StringUtils.isNotBlank(status)) {
                queryWrapper1.eq("status", status);
            }
        }

        queryWrapper1.in("id",list1);

        Page<Course> page = new Page<>(current,limit);

        System.out.println(courseQuery);

        courseService.page(page, queryWrapper1);

        List<Course> records = page.getRecords();
        long total = page.getTotal();

        return R.ok().data("total",total).data("records",records);
    }
}
